<?php
/**
 * 编辑职位
 *
 * @author Dracowyn
 * @since 2023-11-17 19:39
 */

// 引入配置文件
include_once('config/config.php');
$loginAuth = check_auth();

$id = $_GET['id'] ?? 0;

$jobSql = "SELECT * FROM `pre_job` WHERE `id` = '$id'";

$jobData = find($jobSql);
if (!$jobData) {
	showMsg('职位不存在');
	exit();
}

$depSql = "SELECT * FROM pre_department";
$depData = all($depSql);

if ($_POST) {
	// 接收数据
	$name = trim($_POST['name']);
	$depId = trim($_POST['depId']);

	// 验证数据
	if (empty($name)) {
		showMsg('职位名称不能为空');
		exit();
	}
	if (empty($depId)) {
		showMsg('请选择部门');
		exit();
	}
	$sql = "SELECT name FROM pre_job WHERE name = '$name' AND depid = '$depId' AND id != '$id'";
	$findName = find($sql);
	if ($findName) {
		showMsg('职位名称已存在');
		exit();
	}
	$data = [
		'name' => $name,
		'depid' => $depId,
	];
	$res = update('job', $data, "id = '$id'");
	if ($res['code'] === 0) {
		showMsg($res['msg']);
	} else {
		showMsg('编辑成功', 'jobList.php');
	}
}

?>

<!DOCTYPE html>
<html lang="zh">
<head>
	<?php include_once('common/meta.php'); ?>
    <title>编辑职位</title>
</head>

<body>

<?php include_once('common/header.php'); ?>

<?php include_once('common/menu.php'); ?>

<div class="content">
    <div class="header">
        <h1 class="page-title">编辑职位</h1>
    </div>
    <ul class="breadcrumb">
        <li><a href="index.php">首页</a> <span class="divider">/</span></li>
        <li><a href="adminList.php">职位管理</a> <span class="divider">/</span></li>
        <li class="active">编辑职位</li>
    </ul>

    <div class="container-fluid">
        <div class="row-fluid">

            <div class="btn-toolbar">
                <button class="btn btn-primary" onClick="location='jobList.php'"><i class="icon-list"></i>职位列表
                </button>
                <div class="btn-group">
                </div>
            </div>

            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">
                            <label for="name">职位名称</label>
                            <input type="text" name="name" id="name" class="input-xxlarge"
                                   placeholder="请输入职位名称"
								<?php echo isset($jobData['name']) ? 'value="' . $jobData['name'] . '"' : ''; ?>
                                   required/>
                            <label for="depId">部门</label>
                            <select name="depId" id="depId" class="input-xlarge">
                                <option value="0">请选择</option>
								<?php foreach ($depData as $item) { ?>
									<option value="<?php echo $item['id']; ?>" <?php echo $jobData['depid'] == $item['id'] ? 'selected' : ''; ?>><?php echo $item['name']; ?></option>
								<?php } ?>
                            </select>
                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交"/>
                        </form>
                    </div>
                </div>
            </div>
			<?php include_once('common/footer.php'); ?>
        </div>
    </div>
</div>
</body>
<?php include_once('common/script.php'); ?>
</html>
